package com.gz.moreaseManager.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.config.SelfUserDetails;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.modules.entity.Recruit;
import com.gz.moreaseManager.modules.service.IRecruitService;
import com.gz.moreaseManager.utils.DateUtil;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import com.gz.moreaseManager.core.base.BaseController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 招聘信息 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-26
 */
@RestController
@RequestMapping("/recruit")
public class RecruitController extends BaseController {

    @Autowired
    private IRecruitService recruitService;


    @RequestMapping("list")
    public ModelAndView list(){
        return new ModelAndView("modules/recruit/list");
    }

    @GetMapping("add")
    public ModelAndView add(){
        return new ModelAndView("modules/recruit/add");
    }

    @GetMapping("update")
    public ModelAndView update(Integer id){
        Recruit recruit = recruitService.selectById( id );
        ModelAndView module = new ModelAndView("modules/recruit/update");
        module.addObject("recruit",recruit);
        return module;
    }

    @GetMapping("listPage")
    public PageResult<Recruit> findPageLsit(String name, String place,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd")Date startDate,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                          Integer pageNum, Integer pageSize){
        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<Recruit>();
        if( UtilTool.isNotBlank( name ))
            wrapper.like("name",name);
        if( UtilTool.isNotBlank( place ))
            wrapper.like("place",place);
        if( UtilTool.isNotBlank( startDate ) ){
            if( UtilTool.isNotBlank( endDate ) && startDate.getTime() == endDate.getTime() ){
                wrapper.between("release_time",startDate,DateUtil.addDays(startDate,1));
            }else{
                wrapper.between("release_time",startDate,
                        UtilTool.isNotBlank(endDate)?DateUtil.addDays(endDate,1):DateUtil.addDays(new Date(),1));
            }
        }

        if( UtilTool.isBlank(startDate) && UtilTool.isNotBlank( endDate )){
            wrapper.le("release_time",DateUtil.addDays(endDate,1));
        }
        //排序
        wrapper.orderBy("release_time",false);
        Page<Recruit> data = recruitService.selectPage(paramsPage, wrapper);
        return new PageResult(0, null, data.getTotal(), data.getRecords());

    }

    /**
     * 新增招聘
     * @param recruit
     * @return
     */
    @PostMapping("addRecruit")
    public AjaxResult<Boolean> addRecruit(@RequestBody Recruit recruit,Authentication authentication){
        if( recruit == null
                || UtilTool.isBlank( recruit.getName())
                || UtilTool.isBlank( recruit.getPlace()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        recruit.setCreateTime(new Date());
        //创建人
        SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
        recruit.setCreatePerson( userDetails.getUsername());
        Boolean success = recruitService.insert(recruit);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "新增招聘信息失败!");
    }

    /**
     * 获取招聘信息
     * @param id
     * @return
     */
    @GetMapping("findRecruit")
    public AjaxResult<Recruit> findRecruit( Integer id ){
        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Recruit recruit = recruitService.selectById( id );
        return AjaxResult.success(recruit);

    }

    /**
     * 修改招聘
     * @param recruit
     * @return
     */
    @PostMapping("editRecruit")
    public AjaxResult<Boolean> editRecruit(@RequestBody Recruit recruit ){
        if( recruit == null
                || UtilTool.isBlank( recruit.getId())
                || UtilTool.isBlank( recruit.getName())
                || UtilTool.isBlank( recruit.getPlace()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = recruitService.updateById(recruit);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改招聘信息失败!");
    }

    /**
     * 根据ID删除招聘信息
     * @param id
     * @return
     */
    @PostMapping("delete")
    public AjaxResult<Boolean> delete( Integer id){

        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = recruitService.deleteById( id );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除招聘信息失败!");
    }

    /**
     * 获取招聘信息列表
     * @return
     */
    @GetMapping("findRecruitList")
    public AjaxResult<List<Recruit>> findRecruitList(){

        Wrapper wrapper = new EntityWrapper<Recruit>();
        wrapper.lt("release_time",new Date());
        wrapper.ne("status",3);//不显示的不查询出来
        wrapper.orderBy("status",false);
        wrapper.orderBy("release_time",false);
        List<Recruit> recruitList = recruitService.selectList(wrapper);

        if( UtilTool.isNotBlank( recruitList ))
            return AjaxResult.success(recruitList);
        return AjaxResult.build(HttpCode.FAILED, "获取招聘信息列表失败!");
    }
}
